package at.horario.dao.hibernate;

import at.horario.dao.ProfessorDao;
import at.horario.model.Horario;
import at.horario.model.Professor;
import org.hibernate.criterion.DetachedCriteria;
import static org.hibernate.criterion.DetachedCriteria.forClass;
import static org.hibernate.criterion.Projections.rowCount;
import static org.hibernate.criterion.Restrictions.eq;

import java.util.List;

public class ProfessorDaoHibernate extends GenericDaoHibernate<Professor, Long> implements ProfessorDao {
    public ProfessorDaoHibernate() {
        super(Professor.class);
    }

    public Professor findBy(Horario horario, int numeroProfessor) {
        DetachedCriteria criteria = criteria();
        criteria.add(eq("horario", horario));
        criteria.add(eq("numHorario", numeroProfessor));

        List lista = super.getHibernateTemplate().findByCriteria(criteria, 0, 1);
        return lista.isEmpty() ? null : (Professor) lista.get(0);
    }

    public int count(Horario horario) {
        DetachedCriteria criteria = forClass(Professor.class);
        criteria.add(eq("horario", horario));
        criteria.setProjection(rowCount());
        return (Integer) super.getHibernateTemplate().findByCriteria(criteria).get(0);
    }

}